<?php

namespace app\common\model\banxi\organization;

use app\common\enums\AccountTypeEnum;
use app\common\enums\ClassHourBusinessTypeEnum;
use app\common\enums\ClassHourStatusEnum;
use app\common\enums\ClassHourTypeEnum;
use app\common\enums\LevelTypeEnum;
use app\common\enums\OrganizationUserStateEnum;
use app\common\model\banxi\core\CoreClassHourDetail;
use app\common\model\common\BaseModel;
use app\common\service\ConfigService;
use app\common\service\core\CoreAccountClassHourService;
use ba\Exception;
use think\db\exception\PDOException;
use think\exception\ValidateException;
use think\Model;
use think\model\concern\SoftDelete;

/**
 * 机构用户
 */
class OrganizationUser extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_organization_user';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'stateCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];


    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------


    public function getStateCnAttr(string|null $value, array $data): string
    {
        $value = $value ?: ($data['state'] ?? '');
        return (new OrganizationUserStateEnum())->getDescription($value) ?? '';
    }


    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------


    public function createAdmin(array|Model $organization)
    {
        try {
            $organizationId = $organization['id'];
            if ($this->getExistAdmin($organizationId)) {
                return true;
            }
            $adminPhone = $organization['openPhone'];
            $user = [
                'organizationId' => $organizationId,
                'account' => createRandomString('XM', 6),
                'isAdmin' => 1,
                'realName' => $organization['openName'],
                'phone' => $adminPhone,
                'roleId' => '0',
                'roleKey' => 'admin',
                'state' => OrganizationUserStateEnum::E1
            ];
            $salt = createPasswordSalt();
            $user['salt'] = $salt;
            // $password = substr($adminPhone, max(0, strlen($adminPhone) - 6));
            $password = 123456;
            $user['password'] = createPassword($salt, $password);
            $this->save($user);
            $accountClassHourService = new CoreAccountClassHourService();
            $res = $accountClassHourService->getByAccountIdAndType($user['organizationId'], 2);
            if (!$res) {
                throw new ValidateException($accountClassHourService->getError());
            }
        } catch (Exception|PDOException|ValidateException $e) {
            throw new ValidateException($e->getMessage());
        }
        return true;
    }

    public function getExistAdmin(string $organizationId)
    {
        return $this->where(['organizationId' => $organizationId, 'isAdmin' => 1])->count() > 0;
    }

    public static function getByAccount($account)
    {
        return self::where('account|phone', $account)->find();
    }

    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------


}